package com.tuyrt.hi.ui.app.demo.tab

import android.graphics.BitmapFactory
import android.os.Bundle
import android.os.Handler
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.tuyrt.hi.library.util.HiDisplayUtil
import com.tuyrt.hi.ui.app.R
import com.tuyrt.hi.ui.tab.bottom.HiTabBottomLayout
import com.tuyrt.hi.ui.tab.bottom.HiTabBottomInfo
import java.util.*

class HiTabBottomDemoActivity : AppCompatActivity() {

    val hiTabLayout: HiTabBottomLayout by lazy {
        findViewById(R.id.hitablayout)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_hi_tab_bottom_demo)

        initTabBottom()
    }

    private fun initTabBottom() {
        hiTabLayout.setTabAlpha(0.85f)


        val infoList: MutableList<HiTabBottomInfo<*>> = ArrayList()
        val hiHome = HiTabBottomInfo(
            "首页",
            "fonts/iconfont.ttf",
            getString(R.string.if_home),
            null,
            "#ff656565",
            "#ffd44949"
        )
        val hiRecommend = HiTabBottomInfo(
            "收藏",
            "fonts/iconfont.ttf",
            getString(R.string.if_recommend),
            null,
            "#ff656565",
            "#ffd44949"
        )
//        val hiCategory = HiTabBottomInfo(
//            "分类",
//            "fonts/iconfont.ttf",
//            getString(R.string.if_category),
//            null,
//            "#ff656565",
//            "#ffd44949"
//        )

        //删除mipmap-anydpi-v26 xml @https://hymane.itscoder.com/bitmap-factory-decode-return-null/
//            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
//            HiTabInfo info = new HiTabInfo("tab" + i, bitmap, bitmap);
        val bitmap = BitmapFactory.decodeResource(resources, R.drawable.fire, null)
        val hiCategory = HiTabBottomInfo<String>(
            "分类",
            bitmap,
            bitmap,
        )

        val hiChat = HiTabBottomInfo(
            "推荐",
            "fonts/iconfont.ttf",
            getString(R.string.if_chat),
            null,
            "#ff656565",
            "#ffd44949"
        )
        val hiProfile = HiTabBottomInfo(
            "我的",
            "fonts/iconfont.ttf",
            getString(R.string.if_profile),
            null,
            "#ff656565",
            "#ffd44949"
        )
        infoList.add(hiHome)
        infoList.add(hiRecommend)
        infoList.add(hiCategory)
        infoList.add(hiChat)
        infoList.add(hiProfile)
        hiTabLayout.inflateInfo(infoList)

//        Handler().postDelayed(Runnable {
//            infoList.removeAt(1)
//            hiTabLayout.inflateInfo(infoList)
//            hiTabLayout.defaultSelected(hiHome)
//        },3000)

        hiTabLayout.addOnTabSelectedChangeListener { _, _, nextInfo ->
            Toast.makeText(this@HiTabBottomDemoActivity, nextInfo.name, Toast.LENGTH_SHORT).show()
        }

        // 设置默认选中
        hiTabLayout.defaultSelected(hiHome)
        // 改变某个tab的高度
        val findTab = hiTabLayout.findTab(infoList[2])
        findTab?.apply { resetHeight(HiDisplayUtil.dp2px(66f, resources)) }
    }
}